<template>
	<el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false">
		<el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="100px" @keyup.enter="submitHandle()">
			<el-form-item label="主键" prop="id" v-if="false">
				<el-input v-model="dataForm.id" placeholder="主键"></el-input>
			</el-form-item>

			<el-row>
				<el-col :span="12">
					<el-form-item label="选择员工" prop="staffId">
						<el-input v-model="dataForm.staffId" placeholder="员工"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="证书名称" prop="name">
						<el-input v-model="dataForm.name" placeholder="证书名称"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row>
				<el-col :span="12">
					<el-form-item label="证书类型" prop="type">
						<el-tree-select
							v-model="dataForm.type"
							:data="zhengShuLeiXing"
							value-key="id"
							check-strictly
							clearable
							:render-after-expand="false"
							:props="{ value: 'id', label: 'name', children: 'children' }"
							style="width: 100%"
						/>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="发证机构" prop="issuingAuthority">
						<el-input v-model="dataForm.issuingAuthority" placeholder="发证机构"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row>
				<el-col :span="12">
					<el-form-item label="证书编号" prop="code">
						<el-input v-model="dataForm.code" placeholder="证书编号"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="发证日期" prop="issuingDate">
						<ElDatePicker v-model="dataForm.issuingDate" placeholder="请选择" />
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="有效日期" prop="validityDate">
				<ElDatePicker v-model="dataForm.validityDate" placeholder="请选择" />
			</el-form-item>
			<el-form-item label="备注" prop="description">
				<el-input v-model="dataForm.description" placeholder="备注"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="submitHandle()">确定</el-button>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus/es'
import { useCertificateApi, useCertificateSubmitApi } from '@/api/nb/staff/certificate'
import { dictByCode } from '@/hooks/hot'

const emit = defineEmits(['refreshDataList'])

const visible = ref(false)
const dataFormRef = ref()
const zhengShuLeiXing=ref()

const dataForm = reactive({
	id: '',

	staffId: '',
	name: '',
	type: '',
	issuingAuthority: '',
	code: '',
	issuingDate: '',
	validityDate: '',
	description: ''
})

const init = (id?: number) => {
	visible.value = true
	dataForm.id = ''

	// 重置表单数据
	if (dataFormRef.value) {
		dataFormRef.value.resetFields()
	}

	if (id) {
		getCertificate(id)
	}

	dictByCode('ZhengShuLeiXing',zhengShuLeiXing)
}

const getCertificate = (id: number) => {
	useCertificateApi(id).then(res => {
		Object.assign(dataForm, res.data)
	})
}

const dataRules = ref({})

// 表单提交
const submitHandle = () => {
	dataFormRef.value.validate((valid: boolean) => {
		if (!valid) {
			return false
		}

		useCertificateSubmitApi(dataForm).then(() => {
			ElMessage.success({
				message: '操作成功',
				duration: 500,
				onClose: () => {
					visible.value = false
					emit('refreshDataList')
				}
			})
		})
	})
}

defineExpose({
	init
})
</script>
